package com.drojian.page5;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.drojian.demo.R;
import java.util.ArrayList;
import java.util.List;

public class MyPage5Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context context;
    private ArrayList<Pojo> itemList;
    private int normalType = 0;     // 第一种ViewType，正常的item
    private int footType = 1;       // 第二种ViewType，底部的提示View
    private boolean hasMore = true;   // 变量，是否有更多数据
    private boolean fadeTips = false; // 变量，是否隐藏了底部的提示

    private Handler mHandler = new Handler(Looper.getMainLooper()); //获取主线程的Handler

    public MyPage5Adapter(Context context, ArrayList<Pojo> itemList) {
        this.context = context;
        this.itemList = itemList;

    }

    public MyPage5Adapter(Context context, ArrayList<Pojo> itemList, boolean hasMore) {
        this.context = context;
        this.itemList = itemList;
        this.hasMore = hasMore;
    }



    @Override
    public int getItemCount() {
        return itemList.size()+1;
    }

    // 真实的listSize，因为不计上footView
    public int getRealLastPosition() {
        return itemList.size();
    }
    //根据位置判断应该展示哪种类型的视图,正常列表项还是底部加载提示
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == normalType){
            return new MyViewHolder(View.inflate(context, R.layout.page5_item,null));
        }else {
            return new FootHolder(View.inflate(context, R.layout.loading_foot,null));
        }
    }
    //数据和getView绑定
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Pojo currentItem = null;
        // 根据位置得到数据进行绑定
        if (position < itemList.size()) {
            currentItem  = itemList.get(position);
        }
        if(holder instanceof MyViewHolder){
            ((MyViewHolder) holder).bind(currentItem);
        }else{
            // 之所以要设置可见，是因为我在没有更多数据时会隐藏了这个footView
            ((FootHolder) holder).listview_footer_view.setVisibility(View.VISIBLE);
            // 只有获取数据为空时，hasMore为false，所以当我们拉到底部时基本都会首先显示“正在加载更多...”
            if (hasMore == true) {
                // 不隐藏footView提示
                fadeTips = false;
                if (itemList.size() > 0) {
                    // 如果查询数据发现增加之后，就显示正在加载更多
                    ((FootHolder) holder).footer_search_text.setText("正在加载更多...");
                }
            } else {
                if (itemList.size() == 0) {
                    // 如果查询数据发现并没有增加时，就显示没有更多数据了
                    ((FootHolder) holder).footer_search_text.setText("没有更多数据了");

                    // 然后通过延时加载模拟网络请求的时间，在500ms后执行
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // 隐藏提示条
                            ((FootHolder) holder).listview_footer_view.setVisibility(View.GONE);
                            // 将fadeTips设置true
                            fadeTips = true;
                            // hasMore设为true是为了让再次拉到底时，会先显示正在加载更多
                            hasMore = true;
                        }
                    }, 500);
                }
            }
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        //实例化RecyclerView的item
        private TextView title;
        private TextView shareUser;
        private TextView data;
        private TextView type;
        private ImageView image;
        int i ;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.page5_item_image);
            title = itemView.findViewById(R.id.page5_item_tv1);
            shareUser = itemView.findViewById(R.id.page5_item_tv4);
            data = itemView.findViewById(R.id.page5_item_tv3);
            type = itemView.findViewById(R.id.page5_item_tv2);
        }
            //设置点击事件
        public void bind(Pojo itemData) {
            MyPage5Helper helper = new MyPage5Helper(context, "database02.db", null, 1);
            SQLiteDatabase db = helper.getWritableDatabase();

            String title1 = itemData.getTitle();
            title.setText(itemData.getTitle());
            shareUser.setText(itemData.getShareUser());
            data.setText(itemData.getData());
            type.setText(itemData.getType());

            i  = itemData.getImage();
            if (i == 0) {
                image.setImageResource(R.drawable.heart_1);
            } else if (i == 1) {
                image.setImageResource(R.drawable.heart_2);
            }
            ContentValues values = new ContentValues();

            image.setOnClickListener(v -> {
                i = (i + 1) % 2; // 这样每次点击都会在0和1之间切换
                if (i == 0) {
                    image.setImageResource(R.drawable.heart_1);
                    values.put("image", 0);
                } else {
                    image.setImageResource(R.drawable.heart_2);
                    values.put("image", 1);
                }
                // 然后执行数据库更新操作
                db.update("page1", values, "title=?", new String[]{title1});
            });
            title.setOnClickListener(v -> {
               Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("titleURL",Uri.parse(itemData.getOrigin()).toString());

                context.startActivity(intent);
            });
        }


    }
    class FootHolder extends RecyclerView.ViewHolder {
        private RelativeLayout listview_footer_view;
        private TextView footer_search_text;
        public FootHolder(View itemView) {
            super(itemView);
            listview_footer_view = itemView.findViewById(R.id.listview_footer_view);
            footer_search_text = itemView.findViewById(R.id.footer_search_text);
        }
    }
    // 暴露接口，改变fadeTips的方法
    public boolean isFadeTips() {
        return fadeTips;
    }

    // 暴露接口，下拉刷新时，通过暴露方法将数据源置为空
    public void resetDatas() {
        itemList = new ArrayList<>();
    }

    // 暴露接口，更新数据源，并修改hasMore的值，如果有增加数据，hasMore为true，否则为false
    public void updateList(List<Pojo> newDatas, boolean hasMore) {
        // 在原有的数据之上增加新数据
        if (newDatas != null) {
            itemList.addAll(newDatas);
        }
        this.hasMore = hasMore;
        notifyDataSetChanged();
    }


}
